<?php

class Silk_Settings extends Silk_Gateway {

    protected $_dao_class = 'Silk_Dao_Setting';
    protected $_model_class = 'Silk_Setting';
    protected $_primary_key = 'setting_id';

    public function getValue($pKey, $pSiteId=null){
	if (!$pSiteId){
	    $site = Silk::getCurrentSite();
	    $site_id = $site->site_id;
	} else
	    $site_id = $pSiteId;

	$results = $this->fetch(array('name' => $pKey, 'site_id' => $site_id));
	if (count($results) > 0){
	    $setting = $results->current();
	    return $setting->value;
	}

	return null;
    }

    public function setValue($pKey, $pValue, $pSiteId=null){
	if (!$pSiteId){
	    $site = Silk::getCurrentSite();
	    $site_id = $site->site_id;
	} else
	    $site_id = $pSiteId;

	$results = $this->fetch(array('name' => $pKey, 'site_id' => $site_id));
	if (count($results) > 0){
	    $setting = $results->current();
	    $setting->value = $pValue;
	    $setting->save();
	} else {
	    $setting = $this->create(
		array(
		    'site_id' => $site_id,
		    'name' => $pKey,
		    'value' => $pValue
		), true
	    );
	}

	return $setting;
    }

    public function getModelObject($pSetting){

	if (is_string($pSetting) && !is_numeric($pSetting))
	    return $this->fetchByName($pSetting, true);
	else
	    return parent::getModelObject($pSetting);
    }

    public function getModelObjectId($pSetting){

	if (is_string($pSetting) && !is_numeric($pSetting)){
	    $setting = $this->fetchByName($pSetting, true);
	    return $setting->setting_id;
	} else
	    return parent::getModelObject($pSetting);
    }
}

?>